package com.example.travelsn.util;

import android.app.Activity;
import android.app.Application;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.baidu.mapapi.CoordType;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRoutePlanOption;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.example.travelsn.R;
import com.example.travelsn.navigate.WalkingRouteOverlay;

public class NavigateView extends AppCompatActivity {

    private MapView mMapView = null;
    private BaiduMap mBaiduMap = null;

    private RoutePlanSearch nSearch;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //在使用SDK各组件之前初始化context信息，传入ApplicationContext
        SDKInitializer.initialize(this);
        //自4.3.0起，百度地图SDK所有接口均支持百度坐标和国测局坐标，用此方法设置您使用的坐标类型.
        //包括BD09LL和GCJ02两种坐标，默认是BD09LL坐标。
        SDKInitializer.setCoordType(CoordType.BD09LL);
        setContentView(R.layout.navigate_view);
        //获取地图控件引用
        SharedPreferences sharedPreferences = getSharedPreferences("navigation", Activity.MODE_PRIVATE);
        float bLatitude = sharedPreferences.getFloat("blatitude", 30.5f);
        float bLongitude = sharedPreferences.getFloat("blongitude", 120.5f);
        LatLng bLatLng = new LatLng(bLatitude,bLongitude);
        float eLatitude = sharedPreferences.getFloat("elatitude", 30.5f);
        float eLongitude = sharedPreferences.getFloat("elongitude", 128.5f);;
        LatLng eLatLng =  new LatLng(eLatitude,eLongitude);
        String city  = sharedPreferences.getString("city","not logged");
        String end  = sharedPreferences.getString("end","no");
        mMapView = (MapView) findViewById(R.id.navigate_View);
        mBaiduMap = mMapView.getMap();
        MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(13.0f);
        mBaiduMap.setMapStatus(msu);
        //开启交通图
        mBaiduMap.setTrafficEnabled(true);
        Toast.makeText(NavigateView.this,"正在为您规划路线", Toast.LENGTH_SHORT).show();
        walkRoutePlan(bLatLng,end,city);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }
    private void walkRoutePlan(LatLng mstartPoint,String mterminalPoint,String city){
        nSearch = RoutePlanSearch.newInstance();
        OnGetRoutePlanResultListener listener = new OnGetRoutePlanResultListener() {
            @Override
            public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {
                //创建WalkingRouteOverlay实例
                WalkingRouteOverlay walkingRouteOverlay = new WalkingRouteOverlay(mBaiduMap);
                if (walkingRouteResult.getRouteLines().size() > 0) {
                    //获取路径规划数据,(以返回的第一条数据为例)
                    mBaiduMap.clear();
                    //为WalkingRouteOverlay实例设置路径数据
                    walkingRouteOverlay.setData(walkingRouteResult.getRouteLines().get(0));
                    //在地图上绘制WalkingRouteOverlay
                    walkingRouteOverlay.addToMap();
                    walkingRouteOverlay.zoomToSpan();
                }
            }

            @Override
            public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {

            }

            @Override
            public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {

            }

            @Override
            public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {

            }

            @Override
            public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {

            }

            @Override
            public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {

            }
        };
        nSearch.setOnGetRoutePlanResultListener(listener);
        PlanNode stNode = PlanNode.withLocation(mstartPoint);
        PlanNode enNode = PlanNode.withCityNameAndPlaceName(city,mterminalPoint);
        nSearch.walkingSearch((new WalkingRoutePlanOption())
                .from(stNode)
                .to(enNode));
        nSearch.destroy();
    }
}
